<?php

namespace App\Http\Controllers\admin;

use http\Client\Response;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Http\Requests\AddUserVerify;
use App\Http\Requests\SetStatusVerify;
use Illuminate\Support\Facades\Auth;

class User_mangement extends Controller
{
    //获取所有用户
    public function getAllUsers(){
        $res = User::select('id','user_name','account','status','rating')
            ->orderBy('rating','DESC')
            ->paginate(7);
        return Response()->success(200,'成功',$res);
    }

    //新增用户
    public function postAddUser(AddUserVerify $request){
        $req = $request->all();
        $id_now = 0;
        $id_now = Auth::id();
        if($id_now == null){
            return Response()->fail(100, '当前身份未知', null);
        }
        $stu = User::select('status')->where('id',$id_now)->get()->toArray();
        if($stu[0]['status'] == -1){
            $serch = User::where('account','=',$request['account'])
                ->first();
            if($serch != null){
                return response()->fail(100,'账户已存在',null);
            }
            if($req != null) {
                $data = new User();
                $data->user_name = $req['user_name'];
                $data->account = $req['account'];
                $data->status = $req['status'];
                $res = $data->save();
            }
            if($res){
                return response()->success(200,'新增成功',null);
            }else {
                return response()->fail(100,'新增失败,请重试',null);
            }
        } else if($stu[0]['status'] == 0){
            if($req['status'] == -1 || $req['status'] == 0){
                return response()->fail(100,'权限不够,只能设置普通用户',null);
            }else{
                $serch1 = User::where('account','=',$request['account'])
                    ->first();
                if($serch1 != null){
                    return response()->fail(100,'账户已存在',null);
                }
                if($req != null) {
                    $data1 = new User();
                    $data1->user_name = $req['user_name'];
                    $data1->account = $req['account'];
                    $data1->status = $req['status'];
                    $res1 = $data1->save();
                }
                if($res1){
                    return response()->success(200,'新增成功',null);
                }else {
                    return response()->fail(100,'新增失败,请重试',null);
                }
            }
        }else{
            return response()->fail(100,'权限不够',null);
        }
    }

    //重置密码
    public function getResetPass($id){

        $res = User::find($id);
        if($res != null){
            $res->password = encrypt(666666);
            $bool = $res->save();
            if($bool != false){
                return response()->success(200,'重置密码成功',null);
            }else{
                return response()->fail(100,'重置密码失败',null);
            }
        }else{
            return response()->fail(100,'用户不存在',null);
        }
    }

    //设置用户状态
    public function postSetStatus(SetStatusVerify $request,$id){
        $id_now = 0;
        $id_now = Auth::id();
        if($id_now == null){
            return Response()->fail(100, '当前身份未知', null);
        }
        $stu = User::select('status')
            ->where('id',$id_now)
            ->get()
            ->toArray();
        $res = User::find($id);
        if($stu[0]['status'] == -1) {
            if ($res != null) {
                if($res->status == -1){
                    return Response()->fail(100, '权限不够', null);
                }else{
                    $res->status = $request->status;
                    $bool = $res->save();
                    if ($bool) {
                        return Response()->success(200, '修改成功', null);
                    } else {
                        return Response()->fail(100, '修改失败,请重试', null);
                    }
                }
            } else {
                return Response()->fail(100, '用户不存在', null);
            }
        } else if($stu[0]['status'] == 0) {
            if($request->status == -1 || $request->status == 0){
                return Response()->fail(100, '权限不够!', null);
            }else{
                $res = User::find($id);
                if ($res != null) {
                    $res->status = $request->status;
                    $bool = $res->save();
                    if ($bool) {
                        return Response()->success(200, '修改成功', null);
                    } else {
                        return Response()->fail(100, '修改失败,请重试', null);
                    }
                } else {
                    return Response()->fail(100, '用户不存在', null);
                }
            }
        }else{
            return Response()->fail(100, '权限不够!', null);
        }
    }

    //软删除用户
    public function getSoftDelete($id){
        $id_now = 0;
        $id_now = Auth::id();
        if($id_now == null){
            return Response()->fail(100, '当前身份未知', null);
        }
        $stu = User::select('status')->where('id',$id_now)->get()->toArray();
        $res = User::find($id);
        if($stu[0]['status'] == -1) {
            if($res != null){
                if($res->status == -1){
                    return Response()->fail(100, '不能删除超级管理员', null);
                }else {
                    $res->delete();
                    return Response()->success(200, '删除成功', null);
                }
            }else{
                return Response()->fail(100, '删除失败,请重试', null);
            }
        }else if($stu[0]['status'] == 0){
            if($res->status == 0 || $res->status == -1){
                return Response()->fail(100, '权限不够', null);
            }else{
                if($res != null){
                    $res ->delete();
                    return Response()->success(200,'删除成功',null);
                }else{
                    return Response()->fail(100, '删除失败,请重试', null);
                }
            }
        }else{
            return Response()->fail(100, '权限不够', null);
        }
    }

    //恢复用户
    public function getRecovery($id){
        $id_now = 0;
        $id_now = Auth::id();
        if($id_now == null){
            return Response()->fail(100, '当前身份未知', null);
        }
        $stu = User::select('status')->where('id',$id_now)->get()->toArray();
        $res = User::onlyTrashed()->find($id);
        if($stu[0]['status'] == -1){
            if($res != null){
                $bool = $res->restore();
                if($bool != false){
                    return Response()->success(200,'恢复成功',null);
                }else{
                    return Response()->fail(100,'恢复失败',null);
                }
            }else{
                return Response()->fail(100,'已恢复或未被删除',null);
            }
        }else if($stu[0]['status'] == 0){
            if($res->status == -1 || $res->status == 0){
                return Response()->fail(100,'权限不够',null);
            }else{
                $bool = $res->restore();
                if($bool != false){
                    return Response()->success(200,'恢复成功',null);
                }else{
                    return Response()->fail(100,'恢复失败',null);
                }
            }
        }else{
            return Response()->fail(100,'权限不够',null);
        }
    }

    //硬删除用户
    public function getHardDelete($id){
        $id_now = 0;
        $id_now = Auth::id();
        if($id_now == null){
            return Response()->fail(100, '当前身份未知', null);
        }
        $stu = User::select('status')->where('id',$id_now)->get()->toArray();
        $res = User::withTrashed()->find($id);
        if($stu[0]['status'] == -1){
            if($res != null){
                if($res->status == -1){
                    return Response()->fail(100,'不能删除超级管理员',null);
                }else {
                    $bool = $res->forceDelete();
                    if ($bool != false) {
                        return Response()->success(200, '彻底删除成功', null);
                    } else {
                        return Response()->fail(100, '彻底删除失败', null);
                    }
                }
            }else{
                return Response()->fail(100,'此用户不存在',null);
            }
        }else if($stu[0]['status'] == 0){
            if($res->status == -1 || $res->status == 0){
                return Response()->fail(100,'权限不够',null);
            }else{
                if($res != null){
                    $bool = $res->forceDelete();
                    if($bool != false){
                        return Response()->success(200,'彻底删除成功',null);
                    }else{
                        return Response()->fail(100,'彻底删除失败',null);
                    }
                }else{
                    return Response()->fail(100,'此用户不存在',null);
                }
            }
        }else{
            return Response()->fail(100,'权限不够',null);
        }
    }

    //获取被软删除的用户
    public function getSoftDelUserInfo(){
        $res = User::onlyTrashed()->select('id','user_name','account','status','rating')
            ->orderBy('rating','DESC')
            ->paginate(7);
        return Response()->success(200,'成功',$res);
    }
}
